﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace sy2_8
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string input = textBox1.Text;
            // 检查输入是否为单个字符
            if (input.Length != 1)
            {
                MessageBox
        .Show("只能输入 1 个字符！");
                textBox1
        .Text = "";
                textBox1
        .Focus();
                return;
            }
            char c = input[0];
            int baseNum = 0;
            // 判断输入字符类型，确定基础 ASCII 码值
            if (Char.IsNumber(c) && c >= '1' && c <= '9')
            {
                baseNum
        = 49; // '1' 的 ASCII 码是 49
            }
            else if (Char.IsUpper(c))
            {
                baseNum
        = 65; // 'A' 的 ASCII 码是 65
            }
            else if (Char.IsLower(c))
            {
                baseNum
        = 97; // 'a' 的 ASCII 码是 97
            }
            else
            {
                MessageBox
        .Show("您输入的字符不合法，只能是大小写字母或数字 1 - 9！");
                textBox1
        .Text = "";
                textBox1
        .Focus();
                return;
            }
            int loopCount = c - baseNum + 1; // 计算循环次数
            label1
        .Text = "";
            for (int i = 0; i < loopCount; i++)
            {
                for (int j = loopCount; j > i + 1; j--)
                {
                    label1
        .Text += " ";
                }
                // 输出当前行的字符，数量为 2 * i + 1 个
                for (int k = 0; k < 2 * i + 1; k++)
                {
                    label1
        .Text += (char)(baseNum + i);
                }
                label1
        .Text += "\r\n";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // 清空输入文本框和显示三角形的标签
            textBox1
        .Text = "";
            label1
        .Text = "";
            // 将焦点设置到输入文本框
            textBox1
        .Focus();
        }
    }
}
